package net.zjitc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.zjitc.common.page.OrderAndGoodPageData;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Order;
import net.zjitc.entity.OrderWithGoods;
import net.zjitc.exception.OrderException;
import net.zjitc.mapper.OrderMapper;
import net.zjitc.mapper.OrderWithGoodsMapper;
import net.zjitc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 毛钰深
 * @Create 2021/12/27 11:08
 */

@Service
@Transactional
public class IOrderService implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderWithGoodsMapper orderWithGoodsMapper;

    @Override
    public ResponseResult selectAll(String query, int pagenum, int pagesize, Order order) {
        Page<Order> orderPage = new Page<>(pagenum, pagesize);
        //query默认传入""或者为null两种情况
        if (query!=null){
            query=query.trim();
        }
        orderPage = orderMapper.selectPage(orderPage, Wrappers.lambdaQuery(Order.class).like(query != null, Order::getOrder_number, query)
                .eq(order.getUser_id() != null, Order::getUser_id, order.getUser_id()).eq(order.getPay_status() != null, Order::getPay_status, order.getPay_status())
                .eq(order.getIs_send() != null, Order::getIs_send, order.getIs_send()).eq(order.getOrder_fapiao_title() != null, Order::getOrder_fapiao_title, order.getOrder_fapiao_title())
                .eq(order.getOrder_fapiao_company() != null, Order::getOrder_fapiao_company, order.getOrder_fapiao_company()).eq(order.getOrder_fapiao_content() != null, Order::getOrder_fapiao_content, order.getOrder_fapiao_content())
                .eq(order.getConsignee_addr()!=null,Order::getConsignee_addr,order.getConsignee_addr()));
        ResponseResult<OrderAndGoodPageData<List<Order>>> res = new ResponseResult<OrderAndGoodPageData<List<Order>>>();

        OrderAndGoodPageData<List<Order>> orderAndGoodPageData = new OrderAndGoodPageData<>();
        orderAndGoodPageData.setGoods(orderPage.getRecords());
        orderAndGoodPageData.setPagenum(orderPage.getCurrent());
        orderAndGoodPageData.setTotal(orderPage.getTotal());
        res.setSuccess(orderAndGoodPageData);
        return res;
    }

    @Override
    public ResponseResult updateOrder(Order order) throws OrderException {
        //判断传入的参数是否有无效的值,如无效的枚举类型等等,以及因此产生的异常信息处理

        //会自动拼接不为null的字段
        int update = orderMapper.update(order, Wrappers.lambdaUpdate(Order.class).eq(Order::getOrder_id, order.getOrder_id()));
        ResponseResult<Order> res=new ResponseResult<>();
        if (update!=0){
            order=selectById(order.getOrder_id());
            res.setSuccess("修改成功",order);
        }else{
            res.setNotFound("修改失败");
        }
        return res;
    }

    @Override
    public Order selectById(Integer id) throws OrderException {
        Order order=orderMapper.selectById(id);
        if (order==null){
            throw new OrderException("该订单不存在");
        }
        List<OrderWithGoods> orderWithGoods = orderWithGoodsMapper.selectList(Wrappers.lambdaQuery(OrderWithGoods.class).eq(OrderWithGoods::getOrder_id, order.getOrder_id()));
        if (orderWithGoods!=null){
            order.setGoods(orderWithGoods);
        }
        return order;
    }

    @Override
    public ResponseResult updateOrderAddress(Integer id, String consignee_addr) throws OrderException {
        Order order=selectById(id);
        order.setConsigneeAddr(consignee_addr);
        int update = orderMapper.updateById(order);
        ResponseResult<Order> res=new ResponseResult<>();
        if (update!=0){
            res.setSuccess("修改地址成功",order);
        }else{
            res.setNotFound("修改地址失败");
        }
        return res;
    }
}
